package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;

    //在此方法中完成添加操作

    /**
     * 添加菜品口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);

        //dishService.save(dish);
        //将接收到的数据封装到dish中,然后让dishService执行添加操作
        this.save(dishDto);
        //将接收到的数据封装到dish_Flavor中
        Long dishId = dishDto.getId();
        //获取flavors集合,并将dishId封装到该对象中,然后执行添加操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().forEach((flavor) -> {
            flavor.setDishId(dishId);
        });
        dishFlavorService.saveBatch(flavors);//批量保存
    }

    /**
     * 通过id查询菜品和口味
     * @param id
     * @return
     */
    @Override
    public DishDto getWithFlavorById(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //先复制dish中的数据
        BeanUtils.copyProperties(dish, dishDto);
        //再复制dishFlavor中的数据
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 批量删除菜品口味
     * @param ids
     * @return
     */
    @Override
    @Transactional()
    public void deleteByIds(Long[] ids) {
        //1.将id数组转为集合,通过集合删除数据
        List<Long> idList = Arrays.asList(ids);
        //1.1根据id删除dish中的数据
        this.removeByIds(idList);

//        for (int i = 0; i < ids.length; i++) {
//            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(DishFlavor::getDishId, ids[i]);
//            //2根据dishId删除dishFlavor中的数据
//            boolean removebyDishFlavorIds = dishFlavorService.remove(queryWrapper);
//            if (!removebyDishFlavorIds){
//                throw new CustomException("口味删除异常");
//            }
//        }
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId, idList);
        //2根据dishId删除dishFlavor中的数据
        dishFlavorService.remove(queryWrapper);
    }

    /**
     *
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        /*//更新dish表中的数据
        this.updateById(dishDto);

        //更新dish_flavor表中的数据
        //先删
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.in(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorQueryWrapper);
        //后加
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map(flavor -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);*/

        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }
}
